//引入koa框架
let app = require('koa')();
//引入koa-json
let json = require('koa-json');
//引入koa-views
let views = require('koa-views');
//引入koa-onerror;
let onerror = require('koa-onerror');
//引入路由文件
let routers = require('./routes');
// error handler
onerror(app);

// global middlewares
app.use(views('views', {
  root: __dirname + '/views',
  default: 'jade'
}));
//处理post请求
app.use(require('koa-bodyparser')());
app.use(json());
//处理日志
app.use(require('./middlewares/resolve_log'));

//定义静态文件目录
app.use(require('koa-static')(__dirname + '/public'));
//对api数据进行格式化
app.use(require('./middlewares/url_filter')('^/api'));
//引入所有路由文件
app.use(routers.routes(),routers.allowedMethods());

module.exports = app;

